package com.example.dense_rack_server.service.dataBackup;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dense_rack_server.entity.BackupLog;
import com.example.dense_rack_server.entityVo.dataBackup.BackupJobVO;
import com.example.dense_rack_server.enums.BackupCategoryEnum;
import com.example.dense_rack_server.mapper.dataBackup.BackupLogMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 备份记录 服务实现类
 * </p>
 *
 * @author 张彬
 * @since 2025-07-01
 */
@Service
public class BackupLogService extends ServiceImpl<BackupLogMapper, BackupLog> {

    @Resource
    public BackupLogMapper backupLogMapper;

    /**
     * 保存备份记录
     *
     * @return
     */
    public BackupLog saveBackupLog(BackupJobVO jobVO, BackupCategoryEnum categoryEnum) {
        // 保存备份记录
        BackupLog backupLog = new BackupLog();
        backupLog.setNodeId(jobVO.getNodeId());
        backupLog.setCategory(categoryEnum.getMsg());
        backupLog.setDatabaseName(jobVO.getDatabase());
        backupLog.setTablesName(StringUtils.join(jobVO.getTables(), ","));
        backupLog.setDataType(jobVO.getDataType());
        backupLog.setIsCompress(jobVO.getIsCompress());
        backupLog.setIsUpload(jobVO.getIsUpload());
        backupLog.setIsDelete(jobVO.getIsDelete());
        backupLog.setStatus(0);
        // 开始时间
        backupLog.setStartTime(new Date());
        this.baseMapper.insert(backupLog);
        return backupLog;
    }


    /**
     * 获取备份管理列表
     */
    public Map<String, Object> tableList(int page, int limit, String nodeId, String category, String status, String databaseName,
                                         String tablesName) {
//        IPage<Map<String, Object>> pageList = new Page<>(page, limit);//参数一是当前页，参数二是每页个数
//        QueryWrapper<BackupLog> wrapper = new QueryWrapper<>();
//        if (StrUtil.isNotEmpty(nodeId)) {
//            wrapper.like("node_id", nodeId);
//        }
//        if (StrUtil.isNotEmpty(category)) {
//            wrapper.like("category", category);
//        }
//        if (StrUtil.isNotEmpty(status)) {
//            wrapper.like("status", status);
//        }
//        if (StrUtil.isNotEmpty(databaseName)) {
//            wrapper.like("database_name", databaseName);
//        }
//        if (StrUtil.isNotEmpty(tablesName)) {
//            wrapper.like("database_name", tablesName);
//        }
//        wrapper.eq("del_flag", 0).orderByDesc("id");
//        pageList = backupLogMapper.selectMapsPage(pageList, wrapper);
//        Map<String, Object> map = new HashMap<>();
//        map.put("code", 0);
//        map.put("msg", "数据请求成功");
//        map.put("count", pageList.getTotal());
//        map.put("data", pageList.getRecords());
//        return map;


        Map<String, Object> paramsMap = new HashMap<>();
        // 手动处理分页
        int offset = (page - 1) * limit;
        paramsMap.put("page", offset);
        paramsMap.put("limit", limit);
        if (StrUtil.isNotEmpty(nodeId)) {
            paramsMap.put("node_id", nodeId);
        }
        if (StrUtil.isNotEmpty(category)) {
            paramsMap.put("category", category);
        }
        if (StrUtil.isNotEmpty(status)) {
            paramsMap.put("status", status);
        }
        if (StrUtil.isNotEmpty(databaseName)) {
            paramsMap.put("database_name", databaseName);
        }
        if (StrUtil.isNotEmpty(tablesName)) {
            paramsMap.put("tables_name", tablesName);
        }
        List<Map<String, Object>> mapList = backupLogMapper.tableList(paramsMap);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("count", backupLogMapper.tableListCount(paramsMap));
        map.put("data", mapList);
        return map;
    }
}
